import com.github.davidmc24.gradle.plugin.avro.GenerateAvroJavaTask

buildscript {
    ext {

    }

    dependencies {

    }
}


plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.5'
    id 'io.spring.dependency-management' version '1.1.4'
    id 'jacoco'
    id "com.github.davidmc24.gradle.plugin.avro" version "1.9.1"
    id 'pmd' // issues with pmd plugin
}

group = 'org.jmresler.sakila.movies'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven {
        url 'https://packages.confluent.io/maven/'
    }
}

def integrationTest = sourceSets.create('integrationTest')

configurations[integrationTest.implementationConfigurationName].extendsFrom(configurations.testImplementation)
configurations[integrationTest.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly)

tasks.named('test') {
    useJUnitPlatform()
}

def integrationTestTask = tasks.register('integrationTest', Test) {
    description = 'Runs integration tests.'
    group = 'verification'
    useJUnitPlatform()

    testClassesDirs = integrationTest.output.classesDirs
    classpath = configurations[integrationTest.runtimeClasspathConfigurationName] + integrationTest.output

    shouldRunAfter(tasks.named('test'))
}

tasks.named('check') {
    dependsOn(integrationTestTask)
}

ext {
    set('lombokVersion', '1.18.34')
}

dependencies {

    implementation(project(':domain-objects'))

    // Spring Boot
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.kafka:spring-kafka'
    implementation 'org.apache.avro:avro:1.11.3'
    implementation 'io.confluent:kafka-avro-serializer:4.0.0'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'

    // Lombok
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
    testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"

    // Database
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'com.mysql:mysql-connector-j'

    // Test
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.0'

}

tasks.named('test') {
    useJUnitPlatform()
}


test {
    finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
    reports {
        xml.required = false
        csv.required = false
        html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
    }
}



jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.5
            }
        }

        rule {
            enabled = false
            element = 'CLASS'
            includes = ['org.gradle.*']

            limit {
                counter = 'LINE'
                value = 'TOTALCOUNT'
                maximum = 0.3
            }
        }
    }
}

pmd {
    consoleOutput = true
    toolVersion = "7.5.0"
    rulesMinimumPriority = 5
    ignoreFailures = true
    ruleSets = [
            "category/java/bestpractices.xml",
            "category/java/codestyle.xml",
            "category/java/design.xml",
            "category/java/documentation.xml",
            "category/java/errorprone.xml",
            "category/java/multithreading.xml",
            "category/java/performance.xml",
            "category/java/security.xml"
    ]
}



clean {
    delete "logs"
    delete "data"
}


def generateAvro = tasks.register("generateAvro", GenerateAvroJavaTask) {
    source("src/main/resources/avro")
    outputDir = file("src/main/java")
}